package com.boc.image.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import com.boc.bus.control.BusOutputResult;
import com.boc.ibatis.config.IbatisSqlClient;
import com.boc.news.beans.EncryptMD5;

public class Help {
	/**
	 * 取常见问题
	 * @return
	 */
	public List getFAQ(Integer nXh) {
		List result = new ArrayList();
		Map parameter = new HashMap();
		parameter.put("cZt", "1");
		parameter.put("nXh", nXh);
		try {
			result = IbatisSqlClient.sqlMapClient.queryForList("image_t_img_cjwt.ibatorgenerated_selectByPrimaryKey",parameter);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 找回密码
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult forgetMyPass(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			//查看用户名和电子邮件是否存在
			Object cnt = IbatisSqlClient.sqlMapClient.queryForObject("image_t_img_user.userIsExists",m);
			if(Integer.parseInt(cnt.toString()) == 0) {
				result.addValue("PN_RET", "-1");
				result.addValue("PC_MSG", "用户名或电子邮件不正确!");
				return result;
			}
			//生成一个随机密码
			String randPass = this.getRandom();
			String md5 = EncryptMD5.MD5(m.get("cLogin")+randPass);
			
			this.sendEmail(m.get("cLogin").toString(),randPass,m.get("cEmail").toString());
			
			m.put("cLoginpass", md5);
			//更改密码
			IbatisSqlClient.sqlMapClient.update("image_t_img_user.updUserPass",m);
			
			result.addValue("PN_RET", "0");
		} catch (Exception e) {
			result.addValue("PN_RET", "-1");
			result.addValue("PC_MSG", e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 发送密码到邮件
	 * @param c
	 */
	private void sendEmail(String userName,String pass,String emailAddr) {
		// SimpleEmail email = new SimpleEmail();
		HtmlEmail email = new HtmlEmail();
		email.setTLS(true);
		email.setHostName("smtp.qq.com");
		email.setAuthentication("87080234@qq.com", "1qaz2wsx3edc");

		try {
			email.addTo(emailAddr);
			email.setFrom("87080234@qq.com","好多图"); // 我方
			email.setSubject("《好多图》免费图片外链空间密码找回!"); // 标题
			email.setCharset("GB2312"); // 设置Charset
			StringBuffer html = new StringBuffer();
			html.append(userName+"&nbsp;你好,<BR><BR>");
			html.append("您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了一个新的密码. 假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员.<BR><BR>");
			html.append("密码: "+pass+"<BR>");
			html.append("你可以登陆网站更改你的密码!<a href='http://www.hodoto.com'>http://www.hodoto.com</a><BR><BR>");
			html.append("《好多图》免费图片外链空间管理员!");
			email.setHtmlMsg(html.toString());
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 取得一个随机密码
	 * @return
	 */
	private String getRandom() {
		String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		char[] rands = new char[8];
		for (int i = 0; i < 8; i++) {
			int rand = (int) (Math.random() * 36);
			rands[i] = chars.charAt(rand);
		}
		return new String (rands).toLowerCase();
	}
}
